/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simplegeneticalgorithm.genogram;

/**
 *
 * @author eherrerag
 */
public class Family<T> {

    private SimpleNode first;
    private SimpleNode last;
    private SimpleNode current;
    private SimpleNode parent;

    public Family()
    {
        first = null;
        last = first;
        current = first;
        parent = null;
    }

    public SimpleNode parent(){
        return parent;
    }

    public void parent(SimpleNode parent_){
        parent = parent_;
    }
    
    public Family(T data_){
        first = new SimpleNode(data_);
        last = first;
        current = first;
    }

    public SimpleNode next(){
        if ( current != null)
            return current.next();
        else
            return null;
    }

    public SimpleNode previous(){
        if (current != null)
            return current.previous();
        else
            return null;
    }

    public void add(T data_){
        if (first == null){
            first = new SimpleNode(data_);
            last = first;
            current = first;
        }
        else{
            SimpleNode data = new SimpleNode(data_);
            data.previous(last);
            last.next(data);
            last = last.next();
        }

    }

    public void remove(){
        if (current.previous() != null){
            current.previous().next(current.next());

        }
        if (current.next() != null){
            current.next().previous(current.previous());
            current = current.next();
        }else
            current = last;
    }

    @Override
    public String toString(){
        String res = "";

        SimpleNode tmpCurrent = current;

        current = first;
        while (current != null){
            res += current.toString();
            current = current.next();
            if (current != null)
                res += "\n";
        }

        current = tmpCurrent;

        return res;
    }

    public Family family(){
        if (current != null)
            return current.sons();
        else
            return null;
    }

    public SimpleNode first(){
        return first;
    }

    public SimpleNode last(){
        return last;
    }

    public SimpleNode current(){
        return current;
    }
}
